#include <stdio.h>
#include <stdlib.h>
#include "ip.h"
#include "mtcpsink.h"

static class mTcpSinkClass : public TclClass {
public:
        mTcpSinkClass() : TclClass("Agent/TCPSink/mTcpSink") {}
        TclObject* create(int, const char*const*) {
                return (new mTcpSink(new Acker));
        }
} class_mtcpsink;

void mTcpSink::recv(Packet* pkt, Handler*)
{	
	int numToDeliver;
	int numBytes = hdr_cmn::access(pkt)->size();
	
	if(openfile==1){
		pkt_received+=1;
		fprintf(tFile,"%-16d %-16f %-16d\n", pkt_received, Scheduler::instance().clock(), numBytes);
	}
	// number of bytes in the packet just received
	hdr_tcp *th = hdr_tcp::access(pkt);
	/* W.N. Check if packet is from previous incarnation */
	if (th->ts() < lastreset_) {
		// Remove packet and do nothing
		Packet::free(pkt);
		return;
	}
	acker_->update_ts(th->seqno(),th->ts(),ts_echo_rfc1323_);
	// update the timestamp to echo
	
      	numToDeliver = acker_->update(th->seqno(), numBytes);
	// update the recv window; figure out how many in-order-bytes
	// (if any) can be removed from the window and handed to the
	// application
	if (numToDeliver) {
		bytes_ += numToDeliver;
		recvBytes(numToDeliver);
	}
	// send any packets to the application
      	ack(pkt);
	// ACK the packet
	Packet::free(pkt);
	// remove it from the system
}

int mTcpSink::command(int argc, const char*const* argv)
{	
	Tcl& tcl = Tcl::instance();
	if (strcmp(argv[1], "set_filename") == 0) {
		strcpy(tbuf, argv[2]);
		tFile = fopen(tbuf, "w");
		openfile=1;
		return (TCL_OK);
	} 
	
	if (strcmp(argv[1], "closefile") == 0) {	
		fclose(tFile);
		return (TCL_OK);
	}
	
	return (TcpSink::command(argc, argv));
}
